<?php
class ProductSpecController extends BaseController {
    protected $layout = 'layouts.admin';
	public function index()
	{
		$specs = Specificant::where('id','>',0)->paginate(Config::get('app.page_row'));
		$this->layout->content = View::make('admin.product_spec.index')->with(compact('specs'));
	}
	
	public function add()
	{
		if ( $_SERVER['REQUEST_METHOD'] == 'POST') {
			$data = Input::get('data');
			$Spec = new Specificant();
			foreach ($data as $k=>$v) {
				$Spec->$k = $v;
			}
			$Spec->specs = '';
			$Spec->save();
			return Redirect::to( route('admin.product_spec.index') );
		}else{
			$this->layout->content =  View::make('admin.product_spec.add');
		}
	}
	
	public function edit($id)
	{
		$spec = Specificant::find($id);
		if ( $_SERVER['REQUEST_METHOD'] == 'POST') {
			$data = Input::get('data');
			foreach ($data as $k=>$v) {
				$spec->$k = $v;
			}
			$spec->save();
			return Redirect::to( route('admin.product_spec.index') );
		}else{
			$this->layout->content = View::make('admin.product_spec.edit')->with(compact('spec'));
		}
	}
	
	public function drop($id)
	{
		$spec = Specificant::find($id);
		if ($spec ) {
			SpecOption::where('specificant_id', $id)->delete();
			ProductSpec::where('specificant_id', $id)->delete();
			$spec->delete();
			return Redirect::back()->with('success', '删除成功。');
		}else{
			return Redirect::back()->with('error', '没有找到。');
		}
	}
	
	public function option_index($spec_id)
	{
		$spec = Specificant::find($spec_id);
		$this->layout->content = View::make('admin.product_spec.option.index')->with(compact('spec'));
	}
	public function option_add($spec_id)
	{
		$spec = Specificant::find($spec_id);
		if ( $_SERVER['REQUEST_METHOD'] == 'POST') {
			$data = Input::get('data');
			$opt = new SpecOption();
			$opt->specificant_id = $spec_id;
			foreach($data as $k=>$v) {
				$opt->$k = $v;
			}
			$opt->save();
			return Redirect::to( route('admin.product_spec.option.index', ['id'=>$spec_id]) );
		}else{
			$this->layout->content = View::make('admin.product_spec.option.add')->with(compact('spec'));
		}
	}
}